function [a,P] = ImprovedHeuristic(P,S,mode)

% game parameters
global num_players_r;
global num_players_b;

% opponent parameters
global opponent;
pass = opponent.pass;
dribble = opponent.dribble;
get_open = opponent.get_open;
block = opponent.block;
go_to_ball = opponent.go_to_ball;
defensive_line = opponent.defensive_line;
offensive_line = opponent.offensive_line;

if(P{2}=='r')
    num_players = num_players_r;
    num_opponents = num_players_b;
else
    num_players = num_players_b;
    num_opponents = num_players_r;
end

if(PlayerInPossession(P,S,mode))
        [a,P] = PassOrDribble(P,S,mode);
else
        [a,P] = Random(P,S,mode);
end

end

% function [a,P] = PassOrDribble(P,S,mode)
% % game parameters
% global num_players_r;
% global num_players_b;
% 
% if(P{2}=='r')
%     num_players = num_players_r;
%     num_opponents = num_players_b;
% else
%     num_players = num_players_b;
%     num_opponents = num_players_r;
% end
% 
% P_X=P{1}(1);
% P_Y=P{1}(2);
% P_color=P{2};
% 
% close = 0;
% 
% for l=2:numel(S) % check opponents
%     if(S{l}{2} ~= P_color)
%         Sl=S{l};
%         Sl_X=Sl{1}(1);
%         Sl_Y=Sl{1}(2);
% 
%         if(abs(Sl_X-P_X) + abs(Sl_Y-P_Y) <16 &&  abs(Sl_X-(-58)) < abs(P_X-(-58)) )    % close - do not dribble - dangerous
%             close=1;
% 
%             k = round(rand_num(0.5,num_players+0.5)); % choose a random team-mate to pass to
%             [a,P] = PassBall(P,S,mode,k);
%             SaveMemory(P,true,1+k,0);
%             break;
%         end
%     end
% end
% 
% if(~close) % just dribble
%     [a, P] = Dribble(P);
%     SaveMemory(P,false,1,0);
% end
% 
% end